/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Navigation接口实现                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Repository.SqlServer                                  
*│　类    名： NavigationRepository                                      
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.DbHelper;
using NetCoreCMS.IRepository;
using NetCoreCMS.Models;
using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Data;

namespace NetCoreCMS.Repository.SqlServer
{
    public class NavigationRepository:BaseRepository<Navigation,Int32>, INavigationRepository
    {
        public NavigationRepository(IOptionsSnapshot<DbOption> options)
        {
            _dbOption =options.Get("NetCoreCMS");
            if (_dbOption == null)
            {
                throw new ArgumentNullException(nameof(DbOption));
            }
            //_dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString);
        }

        public List<Navigation> GetList(int parent_id, string nav_type)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                var list = _dbConnection.GetList<Navigation>("where nav_type=@nav_type  order by sort_id asc,id desc", new { nav_type = nav_type }).ToList();
                //调用迭代组合成DAGATABLE
                List<Navigation> newData = new List<Navigation>();
                GetChilds(list, newData, parent_id, 0);
                return newData;
            }
        }

        public int UpdateField(int id, string strValue)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                return _dbConnection.Execute("update [" + nameof(Navigation) + "] set " + strValue + " where id=@id", new { id = id });
            }
        }

        /// <summary>
        /// 从内存中取得所有下级类别列表（自身迭代）
        /// </summary>
        private void GetChilds(List<Navigation> oldData, List<Navigation> newData, int parent_id, int class_layer)
        {
            class_layer++;
            var dr = oldData.Where(a => a.parent_id == parent_id).ToList();
            for (int i = 0; i < dr.Count; i++)
            {
                Navigation row = new Navigation();//创建新行
                row = dr[i];
                row.class_layer = class_layer;//赋值深度字段
                newData.Add(row);//添加新行
                //调用自身迭代
                this.GetChilds(oldData, newData, dr[i].id, class_layer);
            }
        }
    }
}